package com.example.controller.User;

import com.example.pojo.DTO.*;
import com.example.pojo.VO.Result;
import com.example.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

import java.util.Random;
import java.util.concurrent.TimeUnit;


@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    //    注册
    @PostMapping("/register")
    public Result Register(@RequestBody RegisterUser registerUser)
    {
        log.info("注册账号{}",registerUser.getEmail());
        return userService.add(registerUser);
    }


//    发送验证码
    @GetMapping("/email")
    public Result sendEmail(String email){
        log.info("验证码");
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("3504144526@qq.com");
        //邮件主题
        mailMessage.setSubject("验证码");
        Random random = new Random();
        int verificationCode = random.nextInt(899999) + 100000;
        mailMessage.setText("验证码: " + verificationCode);
        // TODO 将验证码储存到redis中用于验证
        redisTemplate.opsForValue().set("code",verificationCode,1, TimeUnit.MINUTES);
        //收件人
        mailMessage.setTo(email);
        //发送邮件
        javaMailSender.send(mailMessage);
        return Result.success("发送成功");

    }

//    登录
    @PostMapping("/login")
    public Result UserLogin(@RequestBody LoginDTO loginDTO)
    {
        log.info("登录{}",loginDTO.getUserId());
        return userService.UserLogin(loginDTO);
    }

//    修改密码
    @PutMapping("/updatePwd")
    public Result updatePwd(@RequestBody UpdatePwdDTO pwdDTO)
    {
        log.info("修改密码");
        return userService.updatePwd(pwdDTO);
    }

//    修改email
    @PutMapping("/updateEmail")
    public Result updateEmail(String email,String code)
    {
        log.info("修改email");
//        先查询显示Email，输入新的email提供验证码即可修改
        return userService.updateEamil(email,code);
    }

//  跟新头像
    @PutMapping("/updateAvatar")
    public Result updateAvater(@RequestParam @URL String avater)
    {
        return userService.updateAvater(avater);
    }


//    修改用户基本信息
    @PutMapping("/update")
    public Result update(@RequestBody UpdateBaseDTO baseDTO)
    {
        return userService.updateBase(baseDTO);
    }

//    TODO 使用验证码登录
    @PostMapping("/codeLogin")
    public Result codeLogin(String code,String email)
    {
        return userService.codeLogin(code,email);
    }

//    TODO 使用验证码修改密码
    @PostMapping("/codeUpdatePwd")
    public Result codeUpdatePwd(@RequestBody CodeUpdatePwdDTO codeUpdatePwdDTO)
    {
        return userService.codeUpdatePwd(codeUpdatePwdDTO);
    }

//    查询用户基本信息
    @GetMapping("/getData")
    public Result getData()
    {
        log.info("查询用户基本信息");
        return userService.getData();
    }
}
